package com.wonders.frame.autoGeneration.task;

import com.wonders.frame.autoGeneration.support.TaskContent;
import com.wonders.frame.autoGeneration.task.Core.Task;
import com.wonders.frame.common.util.FileUtil;
import com.wonders.frame.common.util.HumpUtil;

import java.io.File;
import java.util.concurrent.Callable;

/**
 * @author 顾乘瑞
 */
public class GenServiceTask extends Task {
    private static final String PATH = System.getProperty("file.separator") +"src"
            + System.getProperty("file.separator") + "main"
            + System.getProperty("file.separator") + "java"
            + System.getProperty("file.separator") + "com"
            + System.getProperty("file.separator") + "wonders"
            + System.getProperty("file.separator") + "frame"
            + System.getProperty("file.separator") + "service"
            + System.getProperty("file.separator");

    public GenServiceTask(final TaskContent cont){
        super(cont);
    }
    @Override
    public Object call(){
        String tableName = cont.getTableName();
        String entityParamName = HumpUtil.lineToHump(tableName);
        String entityClassName = HumpUtil.tableName2ClassName(tableName);
        String className = entityClassName + "Service";

        String filePath = TaskContent.PROJECT_PATH + PATH;
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String writeFilePath = filePath + "/" + className + ".java";
        StringBuilder content = new StringBuilder("package com.wonders.frame.service;\n" +
                "\n" +
                "import com.gcr.util.StringUtil;\n" +
                "import com.wonders.frame.common.Enum.RemovedCodeEnum;\n" +
                "import com.wonders.frame.repository.core.CoreRepository;\n" +
                "import com.wonders.frame.service.Core.CoreCurdService;\n" +
                "import org.slf4j.Logger;\n" +
                "import org.slf4j.LoggerFactory;\n" +
                "import org.springframework.beans.factory.annotation.Autowired;\n" +
                "import org.springframework.stereotype.Service;\n" +
                "\n" +
                "import java.util.Date;\n" +
                
                "import com.wonders.frame.entity."+entityClassName +";\n" +
                "import com.wonders.frame.repository." + entityClassName + "Repository;\n" +
                "\n" +
                "@Service\n" +
                "public class " + entityClassName + "Service extends CoreCurdService<" + entityClassName + "> {\n" +
                "    Logger log = LoggerFactory.getLogger(" + entityClassName + "Service.class);\n" +
                "\n" +
                "    @Autowired\n" +
                "    private " + entityClassName + "Repository repo;\n" +
                "\n" +
                "    @Override\n" +
                "    protected CoreRepository<" + entityClassName + "> getRepository() {\n" +
                "        return repo;\n" +
                "    }\n" +
                "\n" +
                "    public " + entityClassName + " save(" + entityClassName + " " + entityParamName + ", String userName) {\n" +
                "        if(StringUtil.isEmpty(" + entityParamName + ".getId())) {\n" +
                "            log.debug(\"add " + entityParamName + "\");\n" +
                "            " + entityParamName + ".setRemoved(RemovedCodeEnum.AVAILABLE.getCode());\n" +
                "            " + entityParamName + ".setCrtDate(new Date());\n" +
                "            " + entityParamName + ".setCrtUser(userName);\n" +
                "        }\n" +
                "        " + entityParamName + ".setUpdDate(new Date());\n" +
                "        " + entityParamName + ".setUpdUser(userName);\n" +
                "        return repo.save(" + entityParamName + ");\n" +
                "    }\n" +
                "\n" +
                "}\n");
        FileUtil.createFile(content.toString(), writeFilePath);
        return null;
    }
}
